package 双指针系列;

public class RemoveNthFromEnd {
    public ListNode removeNthFromEnd(ListNode head, int n){
        //用虚拟头节点的原因：防止删除的元素为head
        ListNode dummyHead = new ListNode(0,head);  //虚拟头节点
        ListNode fast = dummyHead;  //快指针
        ListNode slow = dummyHead;  //慢指针
        while (n-- >0 && fast!=null){
            fast = fast.next;
        }
        while(fast.next!=null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummyHead.next;
    }
}
